package com.tentact.dao;

import com.tentact.vo.Employee;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Chf_empDao {
    //通过账号状态查找
    public List<Employee> selectEmp(int employeeStatus){
        String sql="select * from employee where EmployeeStatus = ?";
        Object[] para={employeeStatus};
        RowMapper<Employee> rm=rs -> {
            Employee emp=new Employee();
            try {
                emp.setDepartmentid(rs.getInt("DepartmentId"));
                emp.setUserpwd(rs.getString("UserPwd"));
                emp.setEmployeeid(rs.getInt("EmployeeId"));
                emp.setEmployeestatus(rs.getInt("EmployeeStatus"));
                emp.setRoleid(rs.getInt("Roleid"));

                emp.setEmployeename(rs.getString("EmployeeName"));
                emp.setUsername(rs.getString("UserName"));
                emp.setPhone(rs.getString("Phone"));
                emp.setEmail(rs.getString("Email"));
                emp.setRemark(rs.getString("Remark"));
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return emp;
        };
        return  Dbutil.executeQuery(sql,para,rm);

    }
    //通过姓名或账号名查找
    public List<Employee> findEmpByNameOrUname(String name,String userName){
        System.out.println(name);
        System.out.println(userName);
        // EmployeeName like ? or UserName like ?
        StringBuffer sql=new StringBuffer("select * from employee where 1=1 ");
        List<Object> params =new ArrayList<Object>();
        if (name != null){
            sql.append("and EmployeeName like ? ");
            params.add("%"+name+"%");
        }
        if (userName != null){
            sql.append("and UserName like ? ");
            params.add("%"+userName+"%");
        }
        Object[] paramters =params.toArray();
        RowMapper<Employee> rm=(rs) -> {
            Employee emp=new Employee();
            try {
                emp.setDepartmentid(rs.getInt("DepartmentId"));
                emp.setUserpwd(rs.getString("UserPwd"));
                emp.setEmployeeid(rs.getInt("EmployeeId"));
                emp.setEmployeestatus(rs.getInt("EmployeeStatus"));
                emp.setRoleid(rs.getInt("Roleid"));

                emp.setEmployeename(rs.getString("EmployeeName"));
                emp.setUsername(rs.getString("UserName"));
                emp.setPhone(rs.getString("Phone"));
                emp.setEmail(rs.getString("Email"));
                emp.setRemark(rs.getString("Remark"));
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return emp;
        };
        return  Dbutil.executeQuery(sql.toString(),paramters,rm);

    }
    //通过状态从数组中查找
    public List<Employee> findEmpByStatus(int status,List<Employee> employeeList){
        List<Employee> list=new ArrayList<>();
        Employee emp=null;
        for (Employee e:employeeList) {
            if (e.getEmployeestatus()==status){
                emp=new Employee(e.getEmployeeid(),e.getEmployeename(),e.getUsername(),e.getUserpwd(),e.getPhone(),e.getEmail()
                ,e.getDepartmentid(),e.getRoleid(),e.getEmployeestatus(),e.getRemark());
                list.add(emp);
            }
        }
        return list;
    }
    //通过id改变账号状态
    public int  changeStatusById(int status,int employeeId){
        String sql="UPDATE employee SET EmployeeStatus = ? WHERE EmployeeId = ?";
        Object[] para={status,employeeId};
        return Dbutil.executeUpdate(sql,para);
    }

}
